<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div @mouseleave="moveOutDiv" @mouseenter="isShow=true">
        <h2 class="all">全部商品分类</h2>
        <transition name="sort">
          <div class="sort" v-show="isShow">
            <div class="all-sort-list2" @click="toSearch">
              <div class="item" v-for="c1 in categoryList" :key="c1.categoryId"
                   :class="{item_on:currentId===c1.categoryId}" @mouseenter="moveInItem(c1.categoryId)">
                <h3>
                  <!--                a标签传递属性数据到dataset中-->
                  <a href="javascript:;" :data-category1Id="c1.categoryId" :data-categoryName="c1.categoryName">
                    {{ c1.categoryName }}
                  </a>
                </h3>
                <div class="item-list clearfix">
                  <div class="subitem">
                    <dl class="fore" v-for="c2 in c1.categoryChild" :key="c2.categoryId">
                      <dt>
                        <a href="javascript:;" :data-category2Id="c2.categoryId"
                           :data-categoryName="c2.categoryName">{{ c2.categoryName }}</a>
                        <!--                    <a href="">电子书</a>-->
                      </dt>
                      <dd>
                        <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                          <a href="javascript:;" :data-category3Id="c3.categoryId"
                             :data-categoryName="c3.categoryName">{{ c3.categoryName }}</a>
                          <!--                      <a href="">婚恋/两性</a>-->
                        </em>

                      </dd>
                    </dl>
                  </div>
                </div>
              </div>

            </div>
          </div>
        </transition>

      </div>

      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>

    </div>
  </div>

</template>

<script>
import {mapState} from 'vuex';

///import _ from 'lodash' //完整引入lodash,体积大，效率低
import throttle from 'lodash/throttle'


export default {
  name: "TypeNav",
  data() {
    return {
      //三级菜单移入移出变色判断辨识
      currentId: -1,
      //左侧导航在search初始化是否显示
      isShow: true

    }
  },
  computed: {
    ...mapState({
      categoryList: state => state.home.categoryList,
    }),
  },
  methods: {
    moveInItem: throttle(function (id) {
      this.currentId = id
    }, 30, {'trailing': false}),


    //点击a标签跳转到search页面
    toSearch(event) {
      //从event中找打数据，因为只有a标签我们传递了数据，所以有数据的一定是a标签
      // console.log(event)
      const date_set = event.target.dataset;
      const {category1id, category2id, category3id, categoryname} = date_set;
      //如果categoryname存在则说明是a标签
      if (categoryname) {

        const location = {
          name: 'Search',
        };

        const query = {
          categoryName: categoryname
        };
        if (category1id) {
          query.category1Id = category1id;
        } else if (category2id) {
          query.category2Id = category2id;
        } else {
          query.category3Id = category3id;
        }

        location.query = query;
        //点击a标签跳转之前
        //这里我们做个优化，如果搜索的时候，url中有params参数的时候，我们将params参数携带者一起请求
        // 这样合适实现query，params混合请求，比如在手机页面内，我们可以搜品牌，这样出来的是某品牌的手机，
        // 不然可能出现的是这个品牌的各类商品，不只是手机
        // location.params=this.$route.params
        //跳转到search页面
        this.$router.push(location)

      }


    },

    //如果在Search页面内，移出导航div 左侧导航会消失
    moveOutDiv() {
      this.currentId = -1
      //如果初始化页面的时候不在home页面，则默认左侧导航不显示
      if (this.$route.path !== '/home') {
        this.isShow = false;
      }
    },


  },

  mounted() {
    //初始化页面是发送请求，现在不放在这里了，
    // 因为每次切换时都会发送请求，造成不必要的发送请求，我们把这请求放置到App中，这样请求只发送一次，就可一直使用
    // this.$store.dispatch('getCategoryList');

    //用于判断当前实在那个组件，如果实在search则左侧导航不显示  console.log(this.$route) path: "/home"
    if (this.$route.path !== '/home') {
      this.isShow = false;
    }

  }


}
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      //position: absolute;
      background-color: skyblue;
      //background: #fafafa;
      z-index: 999;
      //移入过渡效果
      &.sort-enter {
        height: 0;
        opacity: 0;
      }

      &.sort-enter-to {
        height: 461px;
        opacity: 1;
      }
    ;

      &.sort-enter-active {
        transition: all .3s;
      }


      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 550px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          //&:hover {
          //  background-color:pink;
          //  .item-list {
          //    display: block;
          //  }
          //}
          &.item_on {
            background-color: pink;

            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}

</style>
